<template>
  <div class="wrapper">
    <div class="form-container">
      <div style="margin: 10px 0; text-align: center; font-size: 32px">学生选课系统(管理员)</div>
      <el-form :model="administrator" :rules="rules" class="login-form" ref="administerForm">
        <div style="padding: 20px">
          <el-form-item prop="aname">
            <el-input placeholder="用户名" size="medium" style="margin: 10px 0" prefix-icon="el-icon-user"
              v-model="administrator.username"></el-input>
          </el-form-item>
          <el-form-item prop="apassword">
            <el-input placeholder="密码" size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password
              v-model="administrator.password"></el-input>
          </el-form-item>

          <router-link to="/login">
            <div style="text-align: right;font-size: 14px;margin-top: -10px;color: #606266;">>>学生登录</div>
          </router-link>

          <el-form-item>
            <div class="btn-login">
              <el-button style="width: 100%;height: 100%" type="primary" size="medium" autocomplete="off"
                @click="doLogin">登录
              </el-button>
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      administrator: {
        username: '',
        password: '',
      },
      rules: {
        aname: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        apassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
      }
    }
  },
  methods: {
    doLogin() {
      let promise = this.$refs.administerForm.validate();
      // 若验证表单通过
      promise.then(() => {
        this.request.post("/admin/login", this.administer).then(res => {
          if (res.msg=="error") {
            this.$message.error("用户名或密码错误")
          } else {
            this.$message.success("登录成功!");
            localStorage.setItem("token", res.token);
            localStorage.setItem("role", "admin");
            localStorage.setItem("username", this.administrator.username);
            setTimeout(() => {
              this.$router.push("/");
            }, 1500);
          }
        })
      }).catch(() => {

      });
    }
  }
}
</script>

<style>
@import "../assets/font/font.css";

.wrapper {
  height: 100vh;
  background-image: linear-gradient(to bottom right,
      #005aa7, #fffde4);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form-container {
  background-color: #fff;
  width: 450px;
  height: 400px;
  padding: 25px;
  border-radius: 20px;
}

.login-form .el-input__inner {
  height: 38px;
}

.btn-login {
  margin-top: 16px;
  height: 42px;
  width: 100%;
}
</style>
